var gulp = require("gulp"),
 	webserver = require("gulp-webserver"),
 	sass = require("gulp-ruby-sass"),
 	livereload = require("gulp-livereload"),
 	uglify = require("gulp-uglify"),
   	sourcemap =require("gulp-sourcemaps"),
 	rename = require("gulp-rename");
 	
 	//自动刷新
 	gulp.task("webserver",function(){
 		gulp.src("dest")
 		.pipe(webserver({
 			livereload:true,
 			open:true
 		}))
 	})


//复制网页
gulp.task("html",function(){
	return gulp.src("src/**/*.html")
	.pipe(gulp.dest("dest"))
})
//4编译sass
gulp.task("sass",function(){
	return sass("src/sass/*.scss",{style:"compressed"})
	.on("error",function(err){
		console.log(err)
	})
	.pipe(gulp.dest("dest/css"))
})

//5 js 压缩文件
gulp.task("js",function(){
	return gulp.src("src/js/*.js")
	.pipe(sourcemap.init())//在js文件刚读入时，将js文件初始化到地图中
	.pipe(uglify())
	.pipe(rename({suffix:".min"}))//重命名  添加后缀
	.pipe(sourcemap.write("map"))
	.pipe(gulp.dest("dest/js"));
})

//3 监听任务
gulp.task("watch",function(){
	gulp.watch("src/**/*.html",["html"]);
	gulp.watch("src/sass/*.scss",["sass"]);
	gulp.watch("src/js/*.js",["js"])
})

gulp.task("default",["webserver","html","sass","js","watch"])
